package com.bwie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.TbProdImageMapper;
import com.bwie.pojo.TbProd;
import com.bwie.pojo.TbProdImage;
import com.bwie.service.TbProdService;
import com.bwie.mapper.TbProdMapper;
import com.bwie.utils.Result;
import com.bwie.vo.AttaVo;
import com.bwie.vo.ProdPageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author andy
* @description 针对表【tb_prod(商品表)】的数据库操作Service实现
* @createDate 2025-05-26 16:48:15
*/
@Service
@Transactional(rollbackFor = Exception.class)
public class TbProdServiceImpl extends ServiceImpl<TbProdMapper, TbProd>
    implements TbProdService{

    @Autowired
    TbProdMapper tbProdMapper;

    @Autowired
    TbProdImageMapper tbProdImageMapper;

    @Override
    public Result getProdList(ProdPageVo prodPageVo) {
        //--1 构造分页参数
        Page<TbProd> page = new Page<>(prodPageVo.getPageNum(), prodPageVo.getPageSize());

        //--2 分页查询
//        QueryWrapper<TbProd> wrapper = new QueryWrapper<>();
//        wrapper.lambda().orderByDesc(TbProd::getProdId);
//        Page<TbProd> tbProdPage = tbProdMapper.selectPage(page, wrapper);
        Page<TbProd> tbProdPage = tbProdMapper.selectProdList(page, prodPageVo);

        return Result.ok().setData(tbProdPage);
    }

    @Override
    public Result addProd(TbProd tbProd) {
        //--1 输入商品spu信息时，需对spu名称做重复、必填的校验
        TbProd prod = tbProdMapper.selectOne(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdSpu, tbProd.getProdSpu())
        );
        if(prod != null) {
            return Result.error().setMsg("SPU商品已存在");
        }
        // 商品名称重复校验
        prod = tbProdMapper.selectOne(
                new QueryWrapper<TbProd>().lambda().eq(TbProd::getProdName, tbProd.getProdName())
        );
        if(prod != null) {
            return Result.error().setMsg("同名商品已存在");
        }

        //--2 添加商品
        // token 解析获取用户名
        tbProd.setProdStatus(0);
        tbProd.setCreateBy("admin");
        tbProd.setCreateTime(new Date());
        tbProdMapper.insert(tbProd);

        return Result.ok();
    }

    @Override
    public Result deleteProdAll(List<TbProd> prodList) {
        for (TbProd prod : prodList) {
            tbProdMapper.deleteById(prod.getProdId());
        }
        return Result.ok();
    }

    @Override
    public Result addProdAtta(AttaVo attaVo) {
        //--1 确定商品是存在
        TbProd tbProd = tbProdMapper.selectById(attaVo.getProdId());
        if(tbProd == null) {
            return Result.error().setMsg("商品不存在");
        }

        //--2 更新商品信息
        tbProd.setProdVideo(attaVo.getProdVideo());
        tbProd.setUpType(attaVo.getUpType());
        tbProd.setUpTime(attaVo.getUpTime());
        tbProd.setDownType(attaVo.getDownType());
        tbProd.setDownTime(attaVo.getDownTime());
        tbProdMapper.updateById(tbProd);

        //--3 添加商品轮播图

        // 添加轮播图片之前，把旧的轮播图片删除
        tbProdImageMapper.delete(new QueryWrapper<TbProdImage>().lambda().eq(TbProdImage::getProdId, tbProd.getProdId()));

        // 添加轮播图片
        if(attaVo.getImageList() != null && attaVo.getImageList().size() > 0) {
            for (String imageUrl : attaVo.getImageList()) {

                TbProdImage tbProdImage = new TbProdImage();
                tbProdImage.setProdId(tbProd.getProdId());
                tbProdImage.setImageUrl(imageUrl);

                tbProdImageMapper.insert(tbProdImage);
            }
        }

        return Result.ok();
    }
}




